﻿﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 排序子节点
    /// </summary>
    public class UGUISortChild : MonoBehaviour
    {
        public RectTransform rt;
        public LayoutType layoutType;
        public LayoutDirection direction;
        public float space;
        private float maxHeight = 0;
        private float maxWidth = 0;
        private float maxSpace;
        private float cellWidth = 0;
        private float cellHeight = 0;
    
        public enum LayoutType
        {
            Up,
            Center,
            Bottom,
        }

        public enum LayoutDirection
        {
            Horizontal,
            Vertical,
        }


        public void Awake()
        {
            if(rt == null)
                rt = GetComponent<RectTransform>();
        }

        public void Sort()
        {
            if(rt == null)
                rt = GetComponent<RectTransform>();
            
            List<RectTransform> childList = new List<RectTransform>();
            maxHeight = 0;
            maxWidth = 0;
            cellWidth = 0;
            cellHeight = 0;
            maxSpace = 0;
            for (int i = 0; i < rt.childCount; i++)
            {
                var rtChild = rt.GetChild(i) as RectTransform;
                childList.Add(rtChild);

                maxWidth += rtChild.rect.width;
                maxHeight += rtChild.rect.height;
                if (i < rt.childCount - 1)
                {
                    maxWidth += space;
                    maxHeight += space;
                    maxSpace += space;
                }
            }

            float addUpWidth = 0;
            float addUpHeight = 0;

            for (int i = 0; i < childList.Count; i++)
            {
                if (direction == LayoutDirection.Horizontal)
                {
                    SortHorizontalCell(ref addUpWidth, childList[i],i);
                }
                else
                {
                    SortVerticalCell(ref addUpHeight, childList[i],i);
                }
            }
        }

        private void SortHorizontalCell(ref float addUpWidth, RectTransform item,int i)
        {
            if (layoutType == LayoutType.Center)
            {
                item.anchoredPosition = new Vector2(-(maxWidth / 2) + addUpWidth + space, 0);
                addUpWidth += item.rect.width;
            }
            else
            {
                item.anchoredPosition = new Vector2(addUpWidth + space, 0);
                addUpWidth += item.rect.width;
            }
        }

        private void SortVerticalCell(ref float addUpHeight, RectTransform item,int i)
        {
            float halfHeight = item.rect.height / 2;
            if (layoutType == LayoutType.Center)
            {
                item.anchoredPosition = new Vector2(0, (maxHeight / 2) + addUpHeight - halfHeight - space * i);
            }
            else
            {
                item.anchoredPosition = new Vector2(0, addUpHeight - halfHeight - space / 2);
            }

            addUpHeight -= item.rect.height;
        }
    }
}